<template>
  <el-card class="box-card">
    <div>
      <!-- 头部按钮 -->
      <el-form>
        <el-form-item>
          <el-button type="success" @click="openInsertWindow()">新增</el-button>
        </el-form-item>
      </el-form>
      <!-- 参数数据表单 -->
      <el-table
        ref="multipleTable"
        :data="dataList"
        border
        style="width: 100%"
      >
        <el-table-column label="参数编号" prop="id" width="80">
        </el-table-column>
        <el-table-column label="参数标识符" prop="key" width="110">
        </el-table-column>
        <el-table-column label="参数内容" prop="value" width="280">
        </el-table-column>
        <el-table-column label="备注" prop="remark" width="300">
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button
              size="mini"
              type="warning"
              @click="deleteConfig(scope.row.id)"
            >删除
            </el-button>
            <el-button
              size="mini"
              type="success"
              @click="openEditWindow(scope.row)"
            >编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 新增按钮打开的页面 -->
      <el-dialog :visible.sync="insertWindow" title="新增参数" width="35%">
        <el-form :model="insertData">
          <el-form-item label="参数标识符" label-width="120px">
            <el-input
              v-model="insertData.key"
              autocomplete="off"
              placeholder="参数标识符"
              style="width: 80%"
            ></el-input>
          </el-form-item>
          <el-form-item label="参数内容" label-width="120px">
            <el-input
              v-model="insertData.value"
              autocomplete="off"
              placeholder="参数内容"
              style="width: 80%"
            ></el-input>
          </el-form-item>
          <el-form-item label="备注" label-width="120px">
            <el-input
              v-model="insertData.remark"
              autocomplete="off"
              placeholder="备注"
              style="width: 80%"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="insertWindow = false;">取 消</el-button>
          <el-button type="primary" @click="insertConfig()">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 编辑按钮打开的页面 -->
      <el-dialog :visible.sync="updateWindow" title="编辑菜单">
        <el-form :model="editData">
          <el-form-item label="参数标识符" label-width="20%">
            <el-input
              v-model="editData.key"
              autocomplete="off"
              placeholder="参数标识符"
              style="width: 80%"
            ></el-input>
          </el-form-item>
          <el-form-item label="参数内容" label-width="20%">
            <el-input
              v-model="editData.value"
              autocomplete="off"
              placeholder="参数内容"
              style="width: 80%"
            ></el-input>
          </el-form-item>
          <el-form-item label="备注" label-width="20%">
            <el-input
              v-model="editData.remark"
              autocomplete="off"
              placeholder="备注"
              style="width: 80%"
              type="textarea"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="updateWindow = false">取 消</el-button>
          <el-button type="primary" @click="updateConfig()">更 新</el-button>
        </div>
      </el-dialog>
      <!-- 页号 -->
      <el-pagination
        :current-page="pageIndex"
        :page-size="pageSize"
        :page-sizes="pageSizes"
        :total="totalPage"
        layout="total ,sizes,prev,pager,next,jumper"
        style="margin-top: 30px"
        @size-change="sizeChangeHandle"
        @current-change="CurrentChangeHandle"
      >
      </el-pagination>
    </div>
  </el-card>
</template>
<script>

export default {
  name: "SysUserOnline",
  data() {
    return {
      //数据参数
      editData: {}, //编辑数据
      insertData: {},//新增数据

      //集合参数
      dataList: [], //页面展示的数据集合

      //页号参数
      pageIndex: 1,
      pageSize: 5,
      pageSizes: [],
      totalPage: 0,
      begin: 0,
      end: this.pageSize - 1,

      //窗口显示参数
      insertWindow: false,
      updateWindow: false,
    };
  },
  methods: {
    //改变数组大小
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.getDataList();
    },
    //改变当前页号
    CurrentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },

    //查询所有数据
    getDataList() {
      const pageInfo = this.$getPageInfo(this.pageSize, this.pageIndex)
      this.$http.post("/config/getPageVo", pageInfo).then((res) => {
        this.cutDataList(res);
      });
    },

    //打开新增页面
    openInsertWindow() {
      this.insertWindow = true;
      this.insertData = {}
    },
    //打开编辑页面
    openEditWindow(item) {
      this.updateWindow = true;
      //回显数据
      this.editData = this.$CopyObject(item);
    },

    //修改参数
    updateConfig() {
      this.updateWindow = false;
      const data = {
        config: this.editData,
        pageBo: this.$getPageInfo(this.pageSize, this.pageIndex)
      }
      this.$http.post("/config/update", data).then((res) => {
        this.$setGlobalConfig(res);
        this.cutDataList(res);
      });
    },
    //删除参数
    deleteConfig(item) {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const data = {
            id: item,
            pageBo: this.$getPageInfo(this.pageSize, this.pageIndex)
          }
          this.$http.post("/config/delete", data).then((res) => {
            this.$setGlobalConfig(res);
            this.cutDataList(res);
          });
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
    //新增参数
    insertConfig() {
      this.insertWindow = false;
      const data = {
        config: this.insertData,
        pageBo: this.$getPageInfo(this.pageSize, this.pageIndex)
      }
      this.$http.post("/config/insert", data).then((res) => {
        this.$setGlobalConfig(res);
        this.cutDataList(res);
      });
    },

    //分页操作
    cutDataList(res) {
      const data = this.$cutPageDataList(res)
      this.dataList = data.dataList
      this.totalPage = data.totalPage
    },

    //初始化页号
    setPageSize() {
      this.pageSizes = this.$getPageSize()
      this.pageSize = this.pageSizes[0]
    },

    //更新全局参数
    updateGlobalConfig() {
      this.$http.post("/config/getMap").then((res) => {
        this.$setGlobalConfig(res)
      });
    },
  },
  mounted() {
    this.setPageSize()
    this.getDataList();
  },
};
</script>
